<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$item=array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'自定义导出',
		'edit'=>array(
			array('type'=>'radio','title'=>'导出类型','name'=>'type','options'=>'type'),
			array('type'=>'select','title'=>'选择活动','name'=>'activity_id','options'=>'activitys'),
			array('type'=>'radio','title'=>'订单状态','name'=>'status','options'=>'status'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time_range'),
		)
	);
	$return['activitys']=pdo_getall('jy_adbaoming_list',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['type']=array('0'=>'导出报名列表','1'=>'导出表单','2'=>'导致报名+表单');
	$return['status']=$this->order_status;
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();	
	$_SESSION['e2cpost']=$post;
	if($post['type']==1 && empty($post['activity_id'])){
		exi('导出表单，必须选择活动！','error');
	}
	if($post['type']==1 && $post['activity_id']){
		$activity=pdo_get('jy_adbaoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['activity_id']));
		$activity['diyfrom']=$activity['diyfrom']?json_decode($activity['diyfrom'],true):array();
		if(count($activity['diyfrom'])==0){
			exi('该活动下没有自定义表单，不能导出表单数据！','error');
		}
	}
	$url=download_url('jy_adbaoming/export/down');
	exi('点确定开始下载','','',$url);
}
if($_RQ['op']=='down'){
	$type=$_SESSION['e2cpost']['type'];
	if(!empty($_SESSION['e2cpost']['activity_id'])){
		$params['activity_id']=intval($_SESSION['e2cpost']['activity_id']);
	}
	$params['status']=intval($_SESSION['e2cpost']['status']);
	if($params['status']==2){
		//$params['status']=array(2,4,5,6,7);
	}
	if(!empty($_SESSION['e2cpost']['paytype'])){
		$params['paytype']=intval($_SESSION['e2cpost']['paytype']);
	}
	//创建时间
	if($_SESSION['e2cpost']['createtime']){
		//$time_range=cfc('tools')->base64_decode($_SESSION['e2cpost']['createtime'],$_SESSION['base_key_str']);
		//$time_range=json_decode($time_range,true);
		$time_range=$_SESSION['e2cpost']['createtime'];
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$list=pdo_getall('jy_adbaoming_order',$params);
	$status=$this->order_status;
	$ispay=array('0'=>'未支付','1'=>'已支付');
	if($type==0){
		$header=array(
			'ordersn'=>'报名编号',
			'activity_title'=>'活动名称',
			'nickname'=>'报名昵称',
			'name'=>'报名人姓名',
			'mobile'=>'联系电话',
			'remark'=>'备注',
			'money'=>'金额',
			'status'=>'状态',
			'createtime'=>'报名时间',
		);
		foreach($list as &$item){
			$item['activity_title']=pdo_getcolumn('jy_adbaoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['activity_id']),'title');
			$item['member']=cfc('core')->member_show($item['user_id']);
			$item['nickname']=$item['member']['nickname'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['status']=$status[$item['status']];
		}
		cfc('tools')->export2excel($header,$list,'报名列表');
	}elseif($type==1){
		$activity=pdo_get('jy_adbaoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['e2cpost']['activity_id']));
		$activity['diyfrom']=$activity['diyfrom']?json_decode($activity['diyfrom'],true):array();
		$header=array();
		$diylist=array();
		$header['ordersn']='报名编号';
		$header['activity_title']='活动名称';
		foreach($activity['diyfrom'] as $k=>$v){
			$header['diy'.$k]=$v['title'];
		}
		$header['status']='状态';
		foreach($list as $item){
			$item['diyfrom']=$item['diyfrom']?json_decode($item['diyfrom'],true):array();
			foreach($item['diyfrom'] as $k=>$v){
				$diyitem['diy'.$k]=$v['data'];
			}
			$diylist[]=$diyitem;
		}
		cfc('tools')->export2excel($header,$diylist,'报名表单列表');
	}elseif($type==2){
		$header=array(
			'ordersn'=>'报名编号',
			'activity_title'=>'活动名称',
			'nickname'=>'报名昵称',
			'name'=>'报名人姓名',
			'mobile'=>'联系电话',
			'remark'=>'备注',
			'money'=>'金额',
			'status'=>'状态',
			'createtime'=>'报名时间',
			'data2'=>'表单内容',
		);
		foreach($list as &$item){
			$item['activity_title']=pdo_getcolumn('jy_adbaoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['activity_id']),'title');
			$item['member']=cfc('core')->member_show($item['user_id']);
			$item['nickname']=$item['member']['nickname'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['diyfrom']=$item['diyfrom']?json_decode($item['diyfrom'],true):array();
			$item['data2']='';
			if(is_array($item['diyfrom'])){
				foreach($item['diyfrom'] as $dk=>&$diyv){
						$item['data2'] .='【'.$diyv['title']."】：";
						if($diyv['type']==7){
							$diyv['data']=$diyv['data']['province'].' '.$diyv['data']['city'].' '.$diyv['data']['district'];
						}
						$item['data2'] .=$diyv['data'];
				}
			}
		}
		cfc('tools')->export2excel($header,$list,'报名列表');
	}
}